package com.hpeng.basic.action;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.hpeng.basic.service.AccountingSubjectManager;
import com.hpeng.basic.service.AssetMeasurementUnitManager;
import com.hpeng.basic.service.ProdStatusMstManager;
import com.hpeng.sys.model.AccountingSubject;
import com.hpeng.sys.model.AssetMeasurementUnit;
import com.hpeng.sys.model.ProdStatusMst;
import com.hpeng.sys.service.Function;
import com.hpeng.util.Pagination;
import com.opensymphony.xwork2.ActionSupport;

@Component("basicDataAction")
@Scope("prototype")
// 其他基础数据管理 主要包括：会计科目 资产计量单位 资产状态
public class BasicDataAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	@Resource
	// 会计科目
	private AccountingSubjectManager accountingSubjectManager;
	@Resource
	// 资产计量单位
	private AssetMeasurementUnitManager assetMeasurementUnitManager;
	@Resource
	// 资产状态
	private ProdStatusMstManager prodStatusMstManager;
	@Resource
	private Function function;
	
	// 用于分页显示
	private int page;
	private String pageLink;
	private Pagination<AccountingSubject> subpagination;
	private Pagination<AssetMeasurementUnit> unitpagination;
	private Pagination<ProdStatusMst> stapagination;
    
	//用到的实体对象
	private AssetMeasurementUnit assetMeasurementUnit;
	private AccountingSubject accountingSubject;
	private ProdStatusMst  prodStatusMst;
	
	//用到的实体对应的编号
	private String unitid;
	private String subid;
	private String statusid;

	//根据条件进行查询
	private String condition;	//条件类型
	private String conditionValue;	//条件值
	private String acstatusValue;
	
	
	private List<String> prodStatusMstList;
	
	private String param;
	private String msg;

	// 分页显示
	public String search() {
		String order = " ";
		if (param.equals("1")) {
			String[] hql1 = { " from AssetMeasurementUnit" };
			this.setUnitpagination(assetMeasurementUnitManager.getPagination(
					page, hql1, order));
			pageLink = unitpagination.getPageLink("search.action?param="
					+ param);
		}
		if (param.equals("2")) {
			String[] hql2 = { " from ProdStatusMst order by seq desc" };
			this.setStapagination(prodStatusMstManager.getPagination(page,
					hql2, order));
			pageLink = stapagination
					.getPageLink("search.action?param=" + param);
		}
		if (param.equals("3")) {
			String[] hql3 = { " from AccountingSubject" };
			this.setSubpagination(accountingSubjectManager.getPagination(page,
					hql3, order));
			pageLink = subpagination
					.getPageLink("search.action?param=" + param);
		}
		return "success";
	}
/********************************资产计量单位*****************************/
	//根据条件不同对资产计量单位进行查询
	public String queryAssetMeasurementUnit(){
		try {
			if(param.equals("1")){
			if(conditionValue!=null){
			       conditionValue = new String(conditionValue.getBytes("ISO8859-1"),
					"UTF-8");
			       }
			String append = "";
			if ("mc".equals(condition)) {
				append = " unitname like ('%"+conditionValue+"%') ";
			}  else if ("zt".equals(condition)) {
				append = " unitstauts like ('%"+acstatusValue+"%') ";
			}
			String[] hql = {" from AssetMeasurementUnit where", append };
			setUnitpagination(assetMeasurementUnitManager.getPagination(page, hql, " "));
			pageLink = unitpagination
					.getPageLink("queryAssetMeasurementUnit.action?param=1&condition=" + condition
							+ "&conditionValue=" + conditionValue+"&acstatusValue=" + acstatusValue);
			}
		} catch (UnsupportedEncodingException e) {
			System.out.println(e.getMessage());
		}
		return "success";
	}
	// 新增资产计量单位
	public String addAssetMeasurementUnit(){
		try {
		    assetMeasurementUnitManager.addAssetMeasurementUnit(assetMeasurementUnit); 
        } catch (Exception e) {
        	e.printStackTrace();
			msg = getText("添加资产计量单位失败！");
		}if (msg == null || "".equals(msg.trim())){
			 msg = getText("添加资产计量单位成功！");
		}
        return search();
	}
   //根据资产计量单位id得到对象
	public String findAssetMeasurementUnit(){
		assetMeasurementUnit = assetMeasurementUnitManager.findAssetMeasurementUnitById(unitid);
		return "success";
	}
	//修改资产计量单位
	public String updateAssetMeasurementUnit(){
		try{
			System.out.println("得到的对象是："+assetMeasurementUnit);
			System.out.println("编号为&&&&&&&&&&&&&&&&&&&&&::::"+assetMeasurementUnit.getUnitid());
		 assetMeasurementUnitManager.updateAssetMeasurementUnit(assetMeasurementUnit);
		}catch(Exception e){
			msg = getText("修改资产计量单位失败！");
		}if (msg == null || "".equals(msg.trim())){
			 msg = getText("修改资产计量单位成功！");
		}
		 return search();
	}
	//删除资产计量单位
	public String deleteAssetMeasurementUnit(){
		try {
			for(int i=0;i<unitid.split(",").length;i++){
				if(assetMeasurementUnitManager.findAssetMeasurementUnitById(unitid.split(",")[i]).getUnitstauts().equals("N")){
				    assetMeasurementUnitManager.delAssetMeasurementUnit(unitid.split(",")[i]);
				}else{
					msg = getText("对不起，你不能删除状态为在用的资产计量单位");
				}
			}
			} catch (RuntimeException e) {
				msg = getText("del.failure");
			}
			if (msg == null || "".equals(msg.trim())) {
				msg = getText("del.success");
			}
		 return search();
	}
	/********************************会计科目*****************************/	
	//根据条件不同对会计科目进行查询
	public String queryAccountingSubject(){
		try {
			if(param.equals("3")){
			if(conditionValue!=null){
			       conditionValue = new String(conditionValue.getBytes("ISO8859-1"),
					"UTF-8");
			       }
			String append = "";
			if ("mc".equals(condition)) {
				append = " subname like ('%"+conditionValue+"%') ";
			}  else if ("zt".equals(condition)) {
				append = " substatus like ('%"+acstatusValue+"%') ";
			}
			String[] hql = {" from AccountingSubject where", append };
			setSubpagination(accountingSubjectManager.getPagination(page, hql, " "));
			pageLink = subpagination
					.getPageLink("queryAccountingSubject.action?param=3&condition=" + condition
							+ "&conditionValue=" + conditionValue+"&acstatusValue=" + acstatusValue);
			}
		} catch (UnsupportedEncodingException e) {
			System.out.println(e.getMessage());
		}
		return "success";
	}
//添加会计科目
	public String addAccountingSubject(){
		try{
		   accountingSubjectManager.addAccountingSubject(accountingSubject);
		} catch (RuntimeException e) {
			msg = getText("save.failure");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("save.success");
		}
		return search();
	}
//根据id查询出对象
	public String findAccountingSubject(){
		accountingSubject = accountingSubjectManager.findAccountingSubjectById(subid);
		return "success";
	}
//更新会计科目数据
	public String updateAccountingSubject(){
		try {	
			accountingSubjectManager.updateAccountingSubject(accountingSubject);
			} catch (RuntimeException rx) {
				msg = getText("update.failure");
			}
			if (msg == null || "".equals(msg.trim())) {
				msg = getText("update.success");
			}
			return search();
	}
//删除会计科目
	public String deleteAccountingSubject(){
		try {
			for(int i=0;i<subid.split(",").length;i++){
				if(accountingSubjectManager.findAccountingSubjectById(subid.split(",")[i]).getSubstatus().equals("N")){
					accountingSubjectManager.delAccountingSubject(subid.split(",")[i]);
				}else{
					msg = getText("对不起，你不能删除状态为在用的会计科目");
				}
				
			}
			} catch (RuntimeException e) {
				e.printStackTrace();
				msg = getText("del.failure");
			}
			if (msg == null || "".equals(msg.trim())) {
				
				msg = getText("del.success");
			}
		return search();
	}
	/********************************资产状态信息*****************************/	
	//得到所有资产主状态
	public String findAllProdStatusMst(){
		prodStatusMstList = function.getStatusList();
		return "success";
	}
	//根据条件不同对资产状态进行查询
	public String queryProdStatusMst(){
		try {
			if(param.equals("2")){
			if(conditionValue!=null){
			       conditionValue = new String(conditionValue.getBytes("ISO8859-1"),
					"UTF-8");
			       }
			String append = "";
			if ("mc".equals(condition)) {
				append = " statusdesc like ('%"+conditionValue+"%') or subdesc like ('%"+conditionValue+"%') ";
			}  else if ("zt".equals(condition)) {
				append = " status like ('%"+acstatusValue+"%') ";
			}
			String[] hql = {" from ProdStatusMst where", append };
			setStapagination(prodStatusMstManager.getPagination(page, hql, " "));
			pageLink = stapagination
					.getPageLink("queryProdStatusMst.action?param=2&condition=" + condition
							+ "&conditionValue=" + conditionValue+"&acstatusValue=" + acstatusValue);
			}
		} catch (UnsupportedEncodingException e) {
			System.out.println(e.getMessage());
		}
		return "success";
	}
//添加资产状态信息
	public String addProdStatusMst(){
		try{
			prodStatusMst.setSeq(function.getMaxSeq("prod_status_mst").toString());
			prodStatusMst.setStatuscode(function.getMainStatusCode(prodStatusMst.getStatusdesc()));
			prodStatusMst.setStatustype("P");
			prodStatusMst.setSubcode(function.getMaxSubCode());
		    prodStatusMstManager.addProdStatusMst(prodStatusMst);
		} catch (RuntimeException e) {
			e.printStackTrace();
			msg = getText("save.failure");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("save.success");
		}
		return search();
	}
//根据id查询出对象
	public String findProdStatusMst(){
		prodStatusMst = prodStatusMstManager.findProdStatusMstById(statusid);
		return "success";
	}
//更新资产状态
	public String updateProdStatusMst(){
		try {	
			prodStatusMst.setStatustype("P");
			prodStatusMst.setStatuscode(function.getMainStatusCode(prodStatusMst.getStatusdesc()));
			prodStatusMstManager.updateProdStatusMst(prodStatusMst);
			} catch (RuntimeException rx) {
				msg = getText("update.failure");
			}
			if (msg == null || "".equals(msg.trim())) {
				msg = getText("update.success");
			}
			return search();
	}
//删除资产状态
	public String deleteProdStatusMst(){
		try {
			for(int i=0;i<statusid.split(",").length;i++){
				if(prodStatusMstManager.findProdStatusMstById(statusid.split(",")[i]).getStatus().equals("N")){
					prodStatusMstManager.delProdStatusMst(statusid.split(",")[i]);
				}else{
					msg = getText("对不起，你不能删除状态为在用的资产状态");
				}
			}
			} catch (RuntimeException e) {
				e.printStackTrace();
				msg = getText("del.failure");
			}
			if (msg == null || "".equals(msg.trim())) {
				msg = getText("del.success");
			}
		return search();
	}
	/********************** set get方法 *************************/
	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public String getPageLink() {
		return pageLink;
	}

	public void setPageLink(String pageLink) {
		this.pageLink = pageLink;
	}

	public Pagination<AccountingSubject> getSubpagination() {
		return subpagination;
	}

	public void setSubpagination(Pagination<AccountingSubject> subpagination) {
		this.subpagination = subpagination;
	}

	public Pagination<AssetMeasurementUnit> getUnitpagination() {
		return unitpagination;
	}

	public void setUnitpagination(
			Pagination<AssetMeasurementUnit> unitpagination) {
		this.unitpagination = unitpagination;
	}

	public Pagination<ProdStatusMst> getStapagination() {
		return stapagination;
	}

	public void setStapagination(Pagination<ProdStatusMst> stapagination) {
		this.stapagination = stapagination;
	}

	public String getParam() {
		return param;
	}

	public void setParam(String param) {
		this.param = param;
	}

	public AssetMeasurementUnit getAssetMeasurementUnit() {
		return assetMeasurementUnit;
	}

	public void setAssetMeasurementUnit(
			AssetMeasurementUnit assetMeasurementUnit) {
		this.assetMeasurementUnit = assetMeasurementUnit;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public AccountingSubject getAccountingSubject() {
		return accountingSubject;
	}

	public void setAccountingSubject(AccountingSubject accountingSubject) {
		this.accountingSubject = accountingSubject;
	}

	public ProdStatusMst getProdStatusMst() {
		return prodStatusMst;
	}

	public void setProdStatusMst(ProdStatusMst prodStatusMst) {
		this.prodStatusMst = prodStatusMst;
	}

	public String getUnitid() {
		return unitid;
	}

	public void setUnitid(String unitid) {
		this.unitid = unitid;
	}

	public String getSubid() {
		return subid;
	}

	public void setSubid(String subid) {
		this.subid = subid;
	}

	public String getStatusid() {
		return statusid;
	}

	public void setStatusid(String statusid) {
		this.statusid = statusid;
	}
	public String getCondition() {
		return condition;
	}
	public void setCondition(String condition) {
		this.condition = condition;
	}
	public String getConditionValue() {
		return conditionValue;
	}
	public void setConditionValue(String conditionValue) {
		this.conditionValue = conditionValue;
	}
	public String getAcstatusValue() {
		return acstatusValue;
	}
	public void setAcstatusValue(String acstatusValue) {
		this.acstatusValue = acstatusValue;
	}
	public List<String> getProdStatusMstList() {
		return prodStatusMstList;
	}
	public void setProdStatusMstList(List<String> prodStatusMstList) {
		this.prodStatusMstList = prodStatusMstList;
	}
}
